home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / image.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  3KB  |  93 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''
  5. Some helper function for loading and converting image data.
  6.  
  7. A PixbufOrSurface is either a GdkPixbuf.Pixbuf or a cairo.Surface. Gtk+ 3.10
  8. added HiDPI support and added APIs which take cairo surfaces. Since we still
  9. want to support older GTK+  we provide some helpers to work on both data
  10. types.
  11.  
  12. Rule of thumb: Every pixbuf which ends up in a surface before getting drawn
  13. needs to be loaded at original_size * scale_factor.
  14.  
  15. To test HiDPI start QL with GDK_SCALE=2.
  16. '''
  17. from gi.repository import GdkPixbuf, Gtk, Gdk, GLib
  18. from quodlibet.util import thumbnails
  19.  
  20. def get_scale_factor(widget):
  21.     '''Returns the scale factor for a Gtk.Widget'''
  22.     if hasattr(widget, 'get_scale_factor'):
  23.         return widget.get_scale_factor()
  24.     return None
  25.  
  26.  
  27. def get_pbosf_for_pixbuf(widget, pixbuf):
  28.     """Returns a cairo surface or the same pixbuf,
  29.     let's call it PixbufOrSurface..
  30.     """
  31.     if hasattr(Gdk, 'cairo_surface_create_from_pixbuf'):
  32.         scale_factor = widget.get_scale_factor()
  33.         if scale_factor == 1:
  34.             return pixbuf
  35.         return None.cairo_surface_create_from_pixbuf(pixbuf, scale_factor, widget.get_window())
  36.     return None
  37.  
  38.  
  39. def pbosf_get_property_name(pbosf):
  40.     '''Gives the property name to use for the PixbufOrSurface.'''
  41.     if pbosf is None or isinstance(pbosf, GdkPixbuf.Pixbuf):
  42.         return 'pixbuf'
  43.     return None
  44.  
  45.  
  46. def set_renderer_from_pbosf(renderer, pbosf):
  47.     '''Set a Gtk.CellRendererPixbuf given a PixbufOrSurface or None'''
  48.     name = pbosf_get_property_name(pbosf)
  49.     renderer.set_property(name, pbosf)
  50.  
  51.  
  52. def set_image_from_pbosf(image, pbosf):
  53.     '''Sets a Gtk.Image given a PixbufOrSurface'''
  54.     if isinstance(pbosf, GdkPixbuf.Pixbuf):
  55.         return image.set_from_pixbuf(pbosf)
  56.     return None.set_from_surface(pbosf)
  57.  
  58.  
  59. def pbosf_render(style_context, cairo_context, pbosf, x, y):
  60.     '''Draws the PixbufOrSurface to the cairo context at (x, y)'''
  61.     if isinstance(pbosf, GdkPixbuf.Pixbuf):
  62.         Gtk.render_icon(style_context, cairo_context, pbosf, x, y)
  63.     else:
  64.         Gtk.render_icon_surface(style_context, cairo_context, pbosf, x, y)
  65.  
  66.  
  67. def pixbuf_from_file(fileobj, boundary, scale_factor = 1):
  68.     '''Returns a pixbuf with the maximum size defined by boundary.
  69.  
  70.     Can raise GLib.GError and return None
  71.     '''
  72.     
  73.     try:
  74.         pixbuf = GdkPixbuf.Pixbuf.new_from_file(fileobj.name)
  75.     except GLib.GError:
  76.         
  77.         try:
  78.             loader = GdkPixbuf.PixbufLoader()
  79.             loader.write(fileobj.read())
  80.             loader.close()
  81.             fileobj.seek(0, 0)
  82.             pixbuf = loader.get_pixbuf()
  83.         except EnvironmentError:
  84.             return None
  85.         
  86.  
  87.  
  88.     (w, h) = boundary
  89.     w *= scale_factor
  90.     h *= scale_factor
  91.     return thumbnails.scale(pixbuf, (w, h), scale_up = False)
  92.  
  93.